<?php
namespace App\Http\Controllers\Web\Member\User;


use App\Http\Controllers\Web\BaseController;
use Illuminate\Http\Request;

class CompanyMaterialController extends BaseController
{
    protected $api_url = [
        'index' => ['get', 'seller/companymaterial'],
        'show' => ['get', 'seller/companymaterial/%d'],
        'store' => ['post', 'seller/companymaterial'],
        'user_show' => ['get', 'member/member/%d'],
        'put' => ['put', ''],
        'delete' => ['delete', ''],
        'showusercompany' => ['get', 'seller/companymaterial/%d/showusercompany'],
        'cate' => ['get', 'cate'],
        'getlistaddress' => ['get', 'getlistaddress/%d'],
        'savecompanymaterial' => ['post', 'seller/companymaterial/savecompanymaterial'],
    ];

    protected $request;

    public function __construct(Request $request)
    {
        $this->request = $request;
    }


    /*
     * 商铺资料视图页
     */
    public function index()
    {
        $ret = http_web($this->api_url['user_show'],[session('user_id')])->send();

        if($ret['data']['data']['identification_type'] != 2){
            return redirect('/member/user/company');
        }
        //公司信息读取
        $ret = http_web($this->api_url['showusercompany'],[session('user_id')])->send();

        if(empty($ret['data']['data']['company'])){
           return redirect('/member/user/company');
        }

        if(!empty($ret['data']['data']['company_material'])){
            $ret['data']['data']['company_material']['business_sum']=explode(" ",$ret['data']['data']['company_material']['business_sum']);
        }
        // 公司性质
        $ent_property = [
            '1' => '国有企业', '2' => '集体企业', '3' => '有限责任公司', '4' => '股份有限公司',
            '5' => '联营企业', '6' => '股份合作公司', '7' => '私营企业', '8' => '港、澳、台商投资企业',
            '9' => '外商投资企业', '10' => '其他'
        ];

        // 主营行业 商品一级分类
        $params = [
            'search' => 'pid:0',
            'searchJoin' => 'and',
            'type' => 'all'
        ];
        $cate_data = http_web($this->api_url['cate'])->send($params);
        $business_trade = $cate_data['data']['list'];

        //区域
        $region_list = http_web($this->api_url['getlistaddress'],['100000'])->send();
        $region_key = [1 => '华东', 2 => '华北', 3 => '华中', 4 => '华南', 5 => '东北', 6 => '西北', 7 => '西南'];

        // 主营产品
        $business_product = null;
        if (!empty(json_decode($ret['data']['data']['company_material']['business_product']))) {
            $business_product = array_chunk(json_decode($ret['data']['data']['company_material']['business_product']), 5);
            foreach ($business_product as $key => $temp_arr) {
                if (count($business_product[$key]) < 5) {
                    $business_product[$key] = array_merge($temp_arr, array_fill(count($temp_arr) - 1, 5 - count($temp_arr), ''));
                }
            }
        } else {
            $business_product = [['', '', '', '', '']];
        }

        //客户群体
        $customer_groups = null;
        if (!empty(json_decode($ret['data']['data']['company_material']['customer_groups']))) {
            $customer_groups = array_chunk(json_decode($ret['data']['data']['company_material']['customer_groups']), 5);
            foreach ($customer_groups as $key => $temp_arr) {
                if (count($customer_groups[$key]) < 5) {
                    $customer_groups[$key] = array_merge($temp_arr, array_fill(count($temp_arr) - 1, 5 - count($temp_arr), ''));
                }
            }
        } else {
            $customer_groups = [['', '', '', '', '']];
        }


        // 采购产品
        $purchase_products = null;
        if (!empty(json_decode($ret['data']['data']['company_material']['purchase_products']))) {
            $purchase_products = array_chunk(json_decode($ret['data']['data']['company_material']['purchase_products']), 5);
            foreach ($purchase_products as $key => $temp_arr) {
                if (count($purchase_products[$key]) < 5) {
                    $purchase_products[$key] = array_merge($temp_arr, array_fill(count($temp_arr) - 1, 5 - count($temp_arr), ''));
                }
            }
        } else {
            $purchase_products = [['', '', '', '', '']];
        }


        // 服务标签
        $ent_service_tag = null;
        if (!empty(json_decode($ret['data']['data']['company_material']['ent_service_tag']))) {
            $ent_service_tag = array_chunk(json_decode($ret['data']['data']['company_material']['ent_service_tag']), 5);
            foreach ($ent_service_tag as $key => $temp_arr) {
                if (count($ent_service_tag[$key]) < 5) {
                    $ent_service_tag[$key] = array_merge($temp_arr, array_fill(count($temp_arr) - 1, 5 - count($temp_arr), ''));
                }
            }
        } else {
            $ent_service_tag = [['', '', '', '', '']];
        }


        // 经营地址
        if ($ret['data']['data']['company_material']['business_address']) {
            $business_address = unserialize($ret['data']['data']['company_material']['business_address']);
            $business_address['regions'] = explode(',', $business_address['regions']);
        } else {
            $business_address = ['regions' => [], 'addr' => ''];
        }
        //联系地址
        if ($ret['data']['data']['company_material']['contact_address']) {
            $contact_address = unserialize($ret['data']['data']['company_material']['contact_address']);
            $contact_address['regions'] = explode(',', $contact_address['regions']);
        } else {
            $contact_address = ['regions' => [], 'addr' => ''];
        }

        $data = ['company_id'=>$ret['data']['data']['company']['id'],'company_name'=>$ret['data']['data']['company']['company_name'],'info'=>$ret['data']['data']['company_material'],'ent_property'=>$ent_property,'business_trade'=>$business_trade,'business_product'=>$business_product,'customer_groups'=>$customer_groups,'region_key'=>$region_key,'regions' => $region_list['data'],'purchase_products'=>$purchase_products,'ent_service_tag'=>$ent_service_tag,'business_address'=>$business_address,'contact_address'=>$contact_address];

//        dd($data);
        return view('web.user.seller.prove_information')->with($data);
    }



    /**
     * 卖家中心 — 商家信息保存
     */
    public function saveInformationAction(Request $request)
    {
        $company_id = intval($request->input('company_id'));
        $company_material_id = intval($request->input('company_material_id'));

        // 提交数据
        $params = [
            'company_id' => !empty($company_id) ? $company_id : '' ,
            'ent_type' => json_encode(array_filter(explode(',', $request->input('gongsileixing')))),                         // 公司类型
            'ent_property' => intval($request->input('gongsixingzhi')),                                         // 公司性质
            'ent_emp_number' => $request->input('yuangongrenshu'),              // 员工人数
            'ent_logo' => $request->input('img_path'),                                           // 公司logo
            'ent_service_tag' =>  !empty($request->input('fuwubiaoqian'))?json_encode(array_filter($request->input('fuwubiaoqian'))):null,        // 服务标签
            'ent_idea' => $request->input('gongsilinian'),                      // 公司理念
            'ent_intro' => $request->input('gongsijianjie'),                    // 公司简介
            'contact_person' => $request->input('lianxiren'),                   // 联系人
            'contact_sex' => intval($request->input('man')),                    // 联系人性别
            'contact_position' => $request->input('zhiwei'),                    // 联系人职位
            'contact_mobile' => $request->input('shoujihao'),                   // 联系人手机
            'contact_tel' => $request->input('quhao') . '-' . $request->input('zuoji'),                   // 座机
            'contact_fax' => $request->input('quhao1') . '-' . $request->input('chuanzhen'),              // 传真
            'contact_address' => serialize(['regions' => $request->input('namedtlxdz'), 'addr' => $request->input('namedtlxdz_addr')]),        // 联系地址
            'contact_postcode' => $request->input('youbian'),                   // 邮编
            'business_address' => serialize(['regions' => $request->input('namedtdz'), 'addr' => $request->input('namedtdz_addr')]),           // 经营地址
            'business_trade' => json_encode(array_filter(explode(',', $request->input('zhuyinghangye')))),               // 主营行业
            'business_model' => $request->input('jingyingmoshi'),               // 经营模式
            'business_product' => json_encode(array_filter($request->input('zhuyingchanpin'))),           // 主营产品
            'customer_groups' => !empty($request->input('kehuqunti'))?json_encode(array_filter($request->input('kehuqunti'))):null,                 // 客户群体
            'sales_region' => json_encode($request->input('xiaoshouquyu')),       // 销售区域
            'business_sum' => serialize(['amount' => intval($request->input('nianyingyee')), 'unit' => $request->input('bizhong')]),           // 年营业额
            'purchase_products' =>  !empty($request->input('caigouchanpin'))?json_encode(array_filter($request->input('caigouchanpin'))):null,      // 采购产品
            'purchase_region' => !empty($request->input('caigouquyu'))?json_encode($request->input('caigouquyu')):null,                         // 采购区域
            'userid' => session('user_id')
        ];

        if(!empty($company_material_id)){
            $params['company_material_id'] = $company_material_id;
        }else{
            $params['company_material_id']='';
        }

        $data = http_web($this->api_url['savecompanymaterial'])->send($params);
        if ($data['code'] == 0) {
            if(!empty($data['data'])){
                session(['user_shop_id'=>$data['data']]);
                session(['user_personal_enterprise'=>1]);
            }
            return redirect('member/user/companymaterial')->withErrors(['操作成功！']);
        } else {
            return redirect('member/user/companymaterial');
//            return redirect('/member/seller/index');
        }
    }

    /**
     * 商铺资料添加
     */
    public function store()
    {

    }



}